﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace Web2014112110
{
    public partial class UpdateSPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "学生修改登录密码";
            string name;
            name = Session["name"].ToString();
            this.lblNowUser.Text =  name;
            this.userName0.Text = name;
            String strCn = "Server=pc.hstc.cn;database=dhd2014112110;uid=2014112110;pwd=19941225";
            SqlConnection cn = new SqlConnection(strCn); //建对象
            try
            {
                cn.Open();
                string strSql = "SELECT   Image FROM  Student  WHERE   (No = N'" + this.userName0.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.Image.ImageUrl = dr["Image"].ToString();
                }
            }
            catch
            {

            }
            finally
            {
                cn.Close();
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] == null)
            {
                Page.Theme = "ThemeFour";
            }
            else if (Session["Theme"].ToString() == "themeFive")
            {
                Page.Theme = "themeFive";
            }
            else if (Session["Theme"].ToString() == "themeTwo")
            {
                Page.Theme = "ThemeTwo";
            }
            else if (Session["Theme"].ToString() == "themeThree")
            {
                Page.Theme = "themeThree";
            }
            else if (Session["Theme"].ToString() == "themeFour")
            {
                Page.Theme = "themeFour";
            }
        }
        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            string password;
            password = Session["password"].ToString();
            if (txtOldPassword.Text == password)
            {
                string name;
                name = Session["name"].ToString();
                this.userName.Text = name;
                string strCn = "server=pc.hstc.cn;database=dhd2014112110;uid=2014112110;pwd=19941225";
                SqlConnection cn = new SqlConnection(strCn);
                try
                {
                    cn.Open();
                    string strNo = this.userName.Text;
                    string strSql = "UPDATE  Student SET Password = N'" + this.txtNewPassword.Text + "' WHERE(No = N'" + this.userName.Text + "' and Password = N'" + this.txtOldPassword.Text + "')";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    int iUpd = cmd.ExecuteNonQuery();
                    if (iUpd < 1)
                    {
                        this.lblInfo1.Text = "学生密码更改失败！";
                        //Response.Write("<script>javascript:alert('更新失败');history.back();</script>");
                    }
                    else
                    {
                        this.lblInfo1.Text = "学生密码更改成功！";
                        //Response.Write("<script>javascript:alert('更新成功');history.back();</script>");
                    }
                }
                catch (Exception ex)
                {
                    this.lblInfo1.Text = ex.Message;
                    //Response.Write("<script>javascript:alert('更新出错');history.back();</script>");
                }
                finally
                {
                    cn.Close();
                }
            }
            else
            {
                this.lblInfo1.Text = "您输入的旧密码不正确，请重新输入！！";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtOldPassword.Text = "";
            this.txtNewPassword.Text = "";
            this.txtNewPassword0.Text = "";
        }
    }
}